/*
 * Copyright (C) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <unistd.h> 
#include "cmsis_os2.h"
#include "lwip/sockets.h"
#include "profile_package.h"
extern int new_fd,sock_fd,red_status,yellow_status,green_status,beep_status,oven_time;
int is_accepted;
/**************************************************
 * deal_report_msg
 * 处理数据，并使用TCP协议上报数据
 * ***********************************************/
static void deal_report_msg()
{
    char *msg;
    uint8_t ret;
    profile_service_t    service;
    profile_kv_t         red;
    profile_kv_t         yellow;
    profile_kv_t         green;
    profile_kv_t         time;

    //配置要发送的数据
    service.event_time = NULL;
    service.service_id = "smart_oven";
    service.service_property = &red;
    service.nxt = NULL;

    red.key = "START";
    red.value = red_status?"ON":"OFF";
    red.type = PROFILE_VALUE_STRING;
    red.nxt = &yellow;

    yellow.key = "SUSPEND";
    yellow.value = yellow_status?"ON":"OFF";
    yellow.type = PROFILE_VALUE_STRING;
    yellow.nxt = &green;

    green.key = "STOP";
    green.value = green_status?"ON":"OFF";
    green.type = PROFILE_VALUE_STRING;
    green.nxt = &time;

    time.key = "TIME";
    time.value = &oven_time;
    time.type = PROFILE_VALUE_INT;
    time.nxt = NULL;

    //打包数据
    msg = profile_package_propertyreport(&service);
    
    //发送数据
    if(send(new_fd, msg, strlen(msg), 0) < 0)
    {
        printf("send error\n");
        close(new_fd);
    }
    return;
}

/**************************************************
 * 任务：report_message_task
 * 上报传感器任务
 * ***********************************************/
int report_message_task(void)
{
    struct sockaddr_in client_sock;
    while(1){
        if(is_accepted == 1 && oven_time > 0){
            deal_report_msg();
        }
        osDelay(30);
    }
    return 0;
}